﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;

public partial class AdminX_Tour_TourAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MessAlert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            this.SetValidate();
            this.BindDataPriorityDropdown();
            this.BindDataToLanguageDropdown();
            this.BindDataDurationToDropdownList();
            this.BindDataStartDestinationToDropdownList();
            this.BindDataArrivedDestinationToDropdownList();
            this.BindDataSafeOffToDropdownList();
            this.BindTourTransport();
            this.BindTourPopular();
            this.BindTourDestination();
            this.BindDataToCountryDropdown();
            this.InitData();
        }
    }

    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();

        lblArrivedDestination.Text = MultipleLanguage.LBL_ArrivedDestination[nDefaultLanguage - 1];
        lblContent.Text = MultipleLanguage.LBL_TOUR_CONTENT[nDefaultLanguage - 1];
        lblDestination.Text = MultipleLanguage.LBL_ArrivedDestination[nDefaultLanguage - 1];
        lblImage.Text = MultipleLanguage.LBL_IMAGE[nDefaultLanguage - 1];
        lblInitContent.Text = MultipleLanguage.LBL_TOUR_INITCONTENT[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblDuration.Text = MultipleLanguage.LBL_DURATION[nDefaultLanguage - 1];
     
        lblSaleOff.Text = MultipleLanguage.SALEOFF[nDefaultLanguage - 1];
        lblStartDestination.Text = MultipleLanguage.LBL_StartDestination[nDefaultLanguage - 1];
        lblTitle.Text = MultipleLanguage.LBL_TITLE[nDefaultLanguage - 1];
        lblTransport.Text = MultipleLanguage.TRANSPORT[nDefaultLanguage - 1];
        lblCountry.Text = MultipleLanguage.LBL_COUNTRY[nDefaultLanguage - 1];
        lblRegion.Text = MultipleLanguage.LBL_REGION[nDefaultLanguage - 1];
        lblTourCat.Text = MultipleLanguage.TOURCATEGORY[nDefaultLanguage - 1];
        lblTourPopular.Text = MultipleLanguage.LBL_TOURPOPULAR[nDefaultLanguage - 1];
        btnCancel.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
        chkActive.Text = MultipleLanguage.STATUS_ACTIVE[nDefaultLanguage - 1];
        chkDisplayOrder.Text = MultipleLanguage.lbl_DesplayOrder[nDefaultLanguage - 1];
        chkIsCustomerTour.Text = MultipleLanguage.LBL_ISCUSTOMERTOUR[nDefaultLanguage - 1];
    }

    #region Relative Functions
    private void SetValidate()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        rfvTitle.ErrorMessage = MessagesValidate.TOUR_TITLE_EMPTY[nDefaultLanguage - 1];
      
        rfvInitContent.ErrorMessage = MessagesValidate.TOUR_INITCONTENT_EMPTY[nDefaultLanguage - 1];
    }
    private void InitData()
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);

        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nItemID > 0)
                    GetData(nItemID);
                GetDataToArrivedDestination(nItemID);
                GetDataToTourTransport(nItemID);
                GetDataToTourPopularDesc(nItemID);
            }
            else
            {
                BindDataToCountryDropdown();
                BindDataToRegionDropdown(0);
                BindDataToCatelogyDropdown(0, 0);
                imgImage.ImageUrl = Config.ROOT + "/Images/no_image.gif";
            }
        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOUR + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }
    private void GetData(int ItemID)
    {
        int nStatus = 0;
        int nDisplayOrder = 0;
       
        Tours objTour = new Tours();
        objTour.LoadById(ItemID);

        int nCountry = 0;
        int nRegion = -1;
        int nTourCat = -1;
        if (objTour.Data.TourCategoryID > 0)
        {
            TourCategory ObjTourCat = new TourCategory();
            ObjTourCat.LoadById(objTour.Data.TourCategoryID);

            Region objRegion = new Region();
            objRegion.LoadById(ObjTourCat.Data.RegionID);

            nCountry = objRegion.Data.CountryID;
            nRegion = ObjTourCat.Data.RegionID;
            nTourCat = objTour.Data.TourCategoryID;
        }
        BindDataToCatelogyDropdown(nRegion, nCountry);
        BindDataToRegionDropdown(nCountry);

        ddlCountry.SelectedValue = ConvertData.ConvertToString(nCountry);
        ddlRegion.SelectedValue = ConvertData.ConvertToString(nRegion);
        ddlTourCat.SelectedValue = ConvertData.ConvertToString(nTourCat);

        ddlStartDestination.SelectedValue = ConvertData.ConvertToString(objTour.Data.StartDestinationID);
        ddlArrivedDestination.SelectedValue = ConvertData.ConvertToString(objTour.Data.ArrivedDestinationID);
        ddlSaleOff.SelectedValue = ConvertData.ConvertToString(objTour.Data.SaleOffID);
        ddlDuration.SelectedValue = ConvertData.ConvertToString(objTour.Data.TourDurationID);
        nDisplayOrder = ConvertData.ConvertToInt(objTour.Data.Priority);
        if (nDisplayOrder > 0)
            chkDisplayOrder.Checked = true;
        else
            chkDisplayOrder.Checked = false;

        nStatus = ConvertData.ConvertToInt(objTour.Data.Status);
        if (nStatus > 0)
            chkActive.Checked = true;
        else
            chkActive.Checked = false;

        if (ConvertData.ConvertToInt(objTour.Data.IsCustomerTour) > 0)
            chkIsCustomerTour.Checked = true;
        else
            chkIsCustomerTour.Checked = false;

        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLang)
        {
            case 1:
                txtTitle.Text = ConvertData.ConvertToString(objTour.Data.Title1);
                txtInitContent.Text = ConvertData.ConvertToString(objTour.Data.InitContent1);
                FTBPrice.IntialText(ConvertData.ConvertToString(objTour.Data.Price1));
                FTBTripHighlight.IntialText(ConvertData.ConvertToString(objTour.Data.TripHighlight1));
                FTBContent.IntialText(ConvertData.ConvertToString(objTour.Data.Content1));
                break;
            case 2:
                txtTitle.Text = ConvertData.ConvertToString(objTour.Data.Title2);
                FTBPrice.IntialText(ConvertData.ConvertToString(objTour.Data.Price2));
                FTBTripHighlight.IntialText(ConvertData.ConvertToString(objTour.Data.TripHighlight2));
                txtInitContent.Text = ConvertData.ConvertToString(objTour.Data.InitContent2);
                FTBContent.IntialText(ConvertData.ConvertToString(objTour.Data.Content2));
                break;
            case 3:
                txtTitle.Text = ConvertData.ConvertToString(objTour.Data.Title3);
                txtInitContent.Text = ConvertData.ConvertToString(objTour.Data.InitContent3);
                   FTBPrice.IntialText(ConvertData.ConvertToString(objTour.Data.Price2));
                FTBTripHighlight.IntialText(ConvertData.ConvertToString(objTour.Data.TripHighlight2));
                FTBContent.IntialText(ConvertData.ConvertToString(objTour.Data.Content3));
                break;
        }
        FTBVideoLink.IntialText(ConvertData.ConvertToString(objTour.Data.VideoLink));
        if (objTour.Data.Image != "")
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TOUR_UPLOAD + objTour.Data.Image.ToString();
        else imgImage.ImageUrl = Config.ROOT + "/Images/no_image.gif";

        DataTable dtbTourImage = new DataTable();
        TourImage objTourImage = new TourImage();
        int nPageCount = 0;
        dtbTourImage = objTourImage.Search(ItemID, 1, 10000, ref nPageCount);
        dtlGallery.DataSource = dtbTourImage;
        dtlGallery.DataBind();
    }
    private void UpdateImage(int nItemID)
    {
        string sFile1 = "";
        string sFile2 = "";
        string sFile3 = "";
        string sFile4 = "";
        string sFile5 = "";
        Session[Config.SESSION_GALLERYID] = ConvertData.ConvertToString(nItemID);
        if (iptFile1.Value != string.Empty) sFile1 = Support.UploadFile(iptFile1, "tour");
        if (iptFile2.Value != string.Empty) sFile2 = Support.UploadFile(iptFile2, "tour");
        if (iptFile3.Value != string.Empty) sFile3 = Support.UploadFile(iptFile3, "tour");
        if (iptFile4.Value != string.Empty) sFile4 = Support.UploadFile(iptFile4, "tour");
        if (iptFile5.Value != string.Empty) sFile5 = Support.UploadFile(iptFile5, "tour");

        TourImage objImage = new TourImage();
        objImage.Data.TourID = nItemID;
        int nResult = 0;

        if (sFile1 != "")
        {
            objImage.Data.Filename = sFile1;
            nResult = objImage.Insert();
        }
        if (sFile2 != "")
        {
            objImage.Data.Filename = sFile2;
            nResult = objImage.Insert();
        }
        if (sFile3 != "")
        {
            objImage.Data.Filename = sFile3;
            nResult = objImage.Insert();
        }
        if (sFile4 != "")
        {
            objImage.Data.Filename = sFile4;
            nResult = objImage.Insert();
        }
        if (sFile5 != "")
        {
            objImage.Data.Filename = sFile5;
            nResult = objImage.Insert();
        }
        GetData(nItemID);

        Session[Config.SESSION_GALLERYID] = "0";
    }
    private eTour TourEntity()
    {
        eTour entityeTour = new eTour();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Tours objTour = new Tours();
            objTour.LoadById(nItemID);
            entityeTour = objTour.Data;
        }
        return entityeTour;
    }
    private eTour SetData()
    {
        eTour eTourEntity = this.TourEntity();
        Tours objTour = new Tours();
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        string sTitle = ConvertData.ConvertToString(ConvertData.RemoveHTMLTag(txtTitle.Text.Trim()));
        string sInitContent = ConvertData.ConvertToString(ConvertData.RemoveHTMLTag(txtInitContent.Text.Trim()));
      
        string sContent = ConvertData.ConvertToString(FTBContent.RenderText());
        string sPrice = ConvertData.ConvertToString(FTBPrice.RenderText());
        string sHighlight = ConvertData.ConvertToString(FTBTripHighlight.RenderText());
        int nCatelogyID = ConvertData.ConvertToInt(ddlTourCat.SelectedValue);
        string sMainContent = ConvertData.ConvertToString(FTBVideoLink.RenderText());
        objTour.Data.VideoLink = sMainContent;
        switch (nCurrentLang)
        {
            case 1:
                objTour.Data.Title1 = sTitle;
                objTour.Data.InitContent1 = sInitContent;
                objTour.Data.Price1 = sPrice;
                objTour.Data.TripHighlight1 = sHighlight;
                objTour.Data.Content1 = sContent;

                objTour.Data.Title2 = ConvertData.ConvertToString(eTourEntity.Title2);
                objTour.Data.InitContent2 = ConvertData.ConvertToString(eTourEntity.InitContent2);
                objTour.Data.Content2 = ConvertData.ConvertToString(eTourEntity.Content2);
                objTour.Data.Price2 = ConvertData.ConvertToString(eTourEntity.Price2);
                objTour.Data.TripHighlight2 = ConvertData.ConvertToString(eTourEntity.TripHighlight2);

                objTour.Data.Title3 = ConvertData.ConvertToString(eTourEntity.Title3);
                objTour.Data.InitContent3 = ConvertData.ConvertToString(eTourEntity.InitContent3);
                objTour.Data.Content3 = ConvertData.ConvertToString(eTourEntity.Content3);
                objTour.Data.Price3 = ConvertData.ConvertToString(eTourEntity.Price3);
                objTour.Data.TripHighlight3 = ConvertData.ConvertToString(eTourEntity.TripHighlight3);
            
                break;
            case 2:
                objTour.Data.Title2 = sTitle;
                objTour.Data.InitContent2 = sInitContent;
                objTour.Data.Price2 = sPrice;
                objTour.Data.TripHighlight2 = sHighlight;
                objTour.Data.Content2 = sContent;

               
                objTour.Data.Title1 = ConvertData.ConvertToString(eTourEntity.Title1);
                objTour.Data.InitContent1 = ConvertData.ConvertToString(eTourEntity.InitContent1);
                objTour.Data.Content1 = ConvertData.ConvertToString(eTourEntity.Content1);
                    objTour.Data.Price1 = ConvertData.ConvertToString(eTourEntity.Price1);
                objTour.Data.TripHighlight1 = ConvertData.ConvertToString(eTourEntity.TripHighlight1);
               
                objTour.Data.Title3 = ConvertData.ConvertToString(eTourEntity.Title3);
                objTour.Data.InitContent3 = ConvertData.ConvertToString(eTourEntity.InitContent3);
                objTour.Data.Content3 = ConvertData.ConvertToString(eTourEntity.Content3);
                    objTour.Data.Price3 = ConvertData.ConvertToString(eTourEntity.Price3);
                objTour.Data.TripHighlight3 = ConvertData.ConvertToString(eTourEntity.TripHighlight3);
                break;
            case 3:
                objTour.Data.Title3 = sTitle;
                objTour.Data.InitContent3 = sInitContent;
                objTour.Data.Price3 = sPrice;
                objTour.Data.TripHighlight3 = sHighlight;
                objTour.Data.Content3 = sContent;

                objTour.Data.Title2 = ConvertData.ConvertToString(eTourEntity.Title2);
                objTour.Data.InitContent2 = ConvertData.ConvertToString(eTourEntity.InitContent2);
                objTour.Data.Content2 = ConvertData.ConvertToString(eTourEntity.Content2);
                  objTour.Data.Price2 = ConvertData.ConvertToString(eTourEntity.Price2);
                objTour.Data.TripHighlight2= ConvertData.ConvertToString(eTourEntity.TripHighlight2);

                objTour.Data.Title1 = ConvertData.ConvertToString(eTourEntity.Title1);
                objTour.Data.InitContent1 = ConvertData.ConvertToString(eTourEntity.InitContent1);
                objTour.Data.Content1 = ConvertData.ConvertToString(eTourEntity.Content1);
                  objTour.Data.Price1 = ConvertData.ConvertToString(eTourEntity.Price1);
                objTour.Data.TripHighlight1 = ConvertData.ConvertToString(eTourEntity.TripHighlight1);
                break;
        }
        objTour.Data.TourCategoryID = nCatelogyID;
        objTour.Data.StartDestinationID = ConvertData.ConvertToInt(ddlStartDestination.SelectedValue);
        objTour.Data.ArrivedDestinationID = ConvertData.ConvertToInt(ddlArrivedDestination.SelectedValue);
        objTour.Data.SaleOffID = ConvertData.ConvertToInt(ddlSaleOff.SelectedValue);
        objTour.Data.TourDurationID = ConvertData.ConvertToInt(ddlDuration.SelectedValue);
        if (chkDisplayOrder.Checked) objTour.Data.Priority = ConvertData.ConvertToInt(ddlPriority.SelectedValue);
        else objTour.Data.Priority = 0;

        if (chkActive.Checked) objTour.Data.Status = Config.STATUS_ACTIVE;
        else objTour.Data.Status = Config.STATUS_INACTIVE;

        if (chkIsCustomerTour.Checked) objTour.Data.IsCustomerTour = 1;
        else objTour.Data.IsCustomerTour = 0;

        if (iptImage.Value != "")
        {
            Support.DeleteFile("tour", eTourEntity.Image);
            objTour.Data.Image = Support.UploadFile(iptImage, "tour");
        }
        if (iptImage.Value == "")
        {
            if (eTourEntity.Image != "")
                objTour.Data.Image = ConvertData.ConvertToString(eTourEntity.Image);
            else objTour.Data.Image = "";
        }
        return objTour.Data;
    }
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    private int SetDataToArrivedDestination(int TourID)
    {
        ArrivedDestination objArrDes = new ArrivedDestination();
        int nresult = 0;
        nresult = objArrDes.DeleteByTourID(TourID);
        if (nresult == 0)
        {
            return 0;
        }

        int nCount = rpt_TourDestination.Items.Count;
        for (int i = 1; i <= nCount; i++)
        {
            CheckBox chk = (CheckBox)rpt_TourDestination.Items[i - 1].FindControl("chkDestination");
            Label lblID = (Label)rpt_TourDestination.Items[i - 1].FindControl("lblDestinationID");
            int nID = ConvertData.ConvertToInt(lblID.Text);
            if (chk.Checked)
            {
                ArrivedDestination obj = new ArrivedDestination();
                obj.Data.TourID = TourID;
                obj.Data.TourDestinationID = nID;
                nresult = obj.Insert();
                if (nresult == 0)
                {
                    return 0;
                }
            }
        }
        return nresult;
    }
    private void BindDataDurationToDropdownList()
    {
        TourDuration obj = new TourDuration();
        DataTable dtb = new DataTable();

        dtb = obj.GetALL();
        ddlDuration.DataSource = dtb;
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLang)
        {
            case 1:
                ddlDuration.DataTextField = "DurationDescription1";
                break;
            case 2:
                ddlDuration.DataTextField = "DurationDescription2";
                break;
            case 3:
                ddlDuration.DataTextField = "DurationDescription3";
                break;
        }
        ddlDuration.DataValueField = "DurationID";
        ddlDuration.DataBind();
        
    }
    private void GetDataToArrivedDestination(int TourID)
    {
        ArrivedDestination objArrivedDes = new ArrivedDestination();
        DataTable dtbArrivedDes = objArrivedDes.Search(TourID);
        int nCount = rpt_TourDestination.Items.Count;
        foreach (DataRow row in dtbArrivedDes.Rows)
        {
            for (int i = 1; i <= nCount; i++)
            {
                CheckBox chk = (CheckBox)rpt_TourDestination.Items[i - 1].FindControl("chkDestination");
                Label lblID = (Label)rpt_TourDestination.Items[i - 1].FindControl("lblDestinationID");
                string sID = ConvertData.ConvertToString(row["TourDestinationID"]);
                if (sID == lblID.Text)
                {
                    chk.Checked = true;
                }
            }

        }
    }
    private void BindTourDestination()
    {
        TourDestination objTourDestination = new TourDestination();
        DataTable dtbTransport = new DataTable();
        dtbTransport = objTourDestination.GetALL();
        rpt_TourDestination.DataSource = dtbTransport;
        rpt_TourDestination.DataBind();
    }
    
    //Save Data to TourTransport
    private int SetDataToTourTransport(int TourID)
    {
        TourTransport objTourTrans = new TourTransport();
        int nresult = 0;
        nresult = objTourTrans.DeleteByTourID(TourID);
        if (nresult == 0)
        {
            return 0;
        }

        int nCount = rpt_TourTransport.Items.Count;
        for (int i = 1; i <= nCount; i++)
        {
            CheckBox chk = (CheckBox)rpt_TourTransport.Items[i - 1].FindControl("chkTourTransport");
            Label lblID = (Label)rpt_TourTransport.Items[i - 1].FindControl("lblTourTransportID");
            int nID = ConvertData.ConvertToInt(lblID.Text);
            if (chk.Checked)
            {
                TourTransport objTourTransport = new TourTransport();
                objTourTransport.Data.TourID = TourID;
                objTourTransport.Data.TransportID = nID;
                nresult = objTourTransport.Insert();
                if (nresult == 0)
                {
                    return 0;
                }
            }
        }
        return nresult;
    }
    private int SetDataToTourPopular(int TourID)
    {
        TourPopularDescription dtgTourPopularsDesc = new TourPopularDescription();
        int nresult = 0;
        nresult = dtgTourPopularsDesc.DeleteByTourID(TourID);
        if (nresult == 0)
        {
            return 0;
        }

        int nCount = rptTourPopular.Items.Count;
        for (int i = 1; i <= nCount; i++)
        {
            CheckBox chk = (CheckBox)rptTourPopular.Items[i - 1].FindControl("chkTourPopular");
            Label lblID = (Label)rptTourPopular.Items[i - 1].FindControl("lblTourPopularID");
            int nID = ConvertData.ConvertToInt(lblID.Text);
            if (chk.Checked)
            {
                TourPopularDescription dtgTourPopularsDescport = new TourPopularDescription();
                dtgTourPopularsDescport.Data.TourID = TourID;
                dtgTourPopularsDescport.Data.TourPopularID = nID;
                nresult = dtgTourPopularsDescport.Insert();
                if (nresult == 0)
                {
                    return 0;
                }
            }
        }
        return nresult;
    }
    private void BindTourTransport()
    {
        Transports objTransports = new Transports();
        DataTable dtbTransport = new DataTable();
        dtbTransport = objTransports.GetALL();
        rpt_TourTransport.DataSource = dtbTransport;
        rpt_TourTransport.DataBind();
    }
    private void BindTourPopular()
    {
        TourPopular objPopular = new TourPopular();
        DataTable dtbPopular = new DataTable();
        dtbPopular = objPopular.GetAll();
        rptTourPopular.DataSource = dtbPopular;
        rptTourPopular.DataBind();
    }
    private void GetDataToTourPopularDesc(int TourID)
    {
        TourPopularDescription objTourTrans = new TourPopularDescription();
        DataTable dtbTrans = objTourTrans.SearchByTourID(TourID);
        int nCount = rptTourPopular.Items.Count;
        foreach (DataRow row in dtbTrans.Rows)
        {
            for (int i = 1; i <= nCount; i++)
            {
                CheckBox chk = (CheckBox)rptTourPopular.Items[i - 1].FindControl("chkTourPopular");
                Label lblID = (Label)rptTourPopular.Items[i - 1].FindControl("lblTourPopularID");

                string sID = ConvertData.ConvertToString(row["TourPopularID"]);
                if (sID == lblID.Text)
                {
                    chk.Checked = true;
                }
            }
        }
    }
    private void GetDataToTourTransport(int TourID)
    {
        TourTransport objTourTrans = new TourTransport();
        DataTable dtbTrans = objTourTrans.Search(TourID);
        int nCount = rpt_TourTransport.Items.Count;
        foreach (DataRow row in dtbTrans.Rows)
        {
            for (int i = 1; i <= nCount; i++)
            {
                CheckBox chk = (CheckBox)rpt_TourTransport.Items[i - 1].FindControl("chkTourTransport");
                Label lblID = (Label)rpt_TourTransport.Items[i - 1].FindControl("lblTourTransportID");

                string sID = ConvertData.ConvertToString(row["TransportID"]);
                if (sID == lblID.Text)
                {
                    chk.Checked = true;
                }
            }
        }
    }
    protected void rpt_TourDestination_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            CheckBox chkDestination = (CheckBox)e.Item.FindControl("chkDestination");
            Label lblDestinationID = (Label)e.Item.FindControl("lblDestinationID");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "TourDestinationID"));
            lblDestinationID.Text = ConvertData.ConvertToString(nID);

            int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
            switch (nCurrentLang)
            {
                case 1:
                    chkDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationTitle1"));
                    break;
                case 2:
                    chkDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationTitle2"));
                    break;
                case 3:
                    chkDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationTitle3"));
                    break;
            }
        }
    }
    protected void rptTourPopular_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            CheckBox chkTourPopular = (CheckBox)e.Item.FindControl("chkTourPopular");
            Label lblTourPopularID = (Label)e.Item.FindControl("lblTourPopularID");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "TourPopularID"));
            lblTourPopularID.Text = ConvertData.ConvertToString(nID);
            int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
            switch (nCurrentLang)
            {
                case 1:
                    chkTourPopular.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Description1"));
                    break;
                case 2:
                    chkTourPopular.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Description2"));
                    break;
                case 3:
                    chkTourPopular.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Description3"));
                    break;
            }
        }
    }
    protected void rpt_TourTransport_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            CheckBox chkTourTransport = (CheckBox)e.Item.FindControl("chkTourTransport");
            Label lblTourTransportID = (Label)e.Item.FindControl("lblTourTransportID");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "TransportID"));
            lblTourTransportID.Text = ConvertData.ConvertToString(nID);
            int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
            switch (nCurrentLang)
            {
                case 1:
                    chkTourTransport.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportName1"));
                    break;
                case 2:
                    chkTourTransport.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportName2"));
                    break;
                case 3:
                    chkTourTransport.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportName3"));
                    break;
            }
        }
    }
    protected void dtlGallery_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        int nDefaulLang = Support.GetDefaultLanguage();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image imgImage = (Image)e.Item.FindControl("imgImage");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TOUR_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Filename"));

            lnkDelete.Text = MultipleLanguage.BTN_DELETE[nDefaulLang - 1];

            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourImageID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaulLang]);
        }
    }
    protected void dtlGallery_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        TourImage objTourImage = new TourImage();
        switch (strCommand)
        {
            case "Delete":
                objTourImage.LoadById(nID);
                Support.DeleteFile("tour", objTourImage.Data.Filename);
                int nDelete = objTourImage.DeleteById(nID);
                GetData(nItemID);
                break;
        }
    }
    #endregion

    #region Click Events
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nDefaultLanguage = Support.GetDefaultLanguage();
        int nTourCat = ConvertData.ConvertToInt(ddlTourCat.SelectedValue);
        if (nTourCat > 0)
        {
            Tours objTour = new Tours();
            objTour.Data = this.SetData();
            string sAction = GetQuerryString(Config.ACTION);
            if (sAction == Config.ACTION_EDIT)
            {
                //Is Edit
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nActionResult = objTour.Update(nItemID);//Update
                int n = SetDataToTourTransport(nActionResult) * SetDataToArrivedDestination(nActionResult) * SetDataToTourPopular(nActionResult);

                if (nActionResult * n > 0)//Kiem tra co loi xay ra
                {
                    UpdateImage(nItemID);
                    MessAlert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage - 1]);                    
                    if (objTour.Data.Image.ToString() != "")
                    {
                        imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TOUR_UPLOAD + objTour.Data.Image.ToString();
                    }
                    if (objTour.Data.Image.ToString() == "")
                    {
                        objTour.Data.Image = Config.ROOT + "/Images/no_image.gif";
                    }
                }
                else
                    MessAlert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage - 1]);
            }
            else
            {
                // Is Insert
                nActionResult = objTour.Insert();
                if (nActionResult > 0)
                {
                    UpdateImage(nActionResult);
                    int n = SetDataToTourTransport(nActionResult) * SetDataToArrivedDestination(nActionResult) * SetDataToTourPopular(nActionResult);
                    if (n > 0)
                    {
                        string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOUR + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                        Response.Redirect(sURL);
                    }
                }
            }
        }
        else
        {
            MessAlert1.ShowMessage(2, MessagesValidate.TOURCAT_SELECT[nDefaultLanguage]);
        }
        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOUR;
        Response.Redirect(sGoToPageMan);
    }
    #endregion

    #region Bind Data Dropdown
    private void BindDataToLanguageDropdown()
    {
        DataTable dtbLanguage = new DataTable();
        Languages objLanguage = new Languages();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();

    }
    private void BindDataStartDestinationToDropdownList()
    {
        TourDestination obj = new TourDestination();
        DataTable dtb = new DataTable();

        dtb = obj.GetALL();
        ddlStartDestination.DataSource = dtb;
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLang)
        {
            case 1:
                ddlStartDestination.DataTextField = "TourDestinationTitle1";
                break;
            case 2:
                ddlStartDestination.DataTextField = "TourDestinationTitle2";
                break;
            case 3:
                ddlStartDestination.DataTextField = "TourDestinationTitle3";
                break;
        }
        ddlStartDestination.DataValueField = "TourDestinationID";
        ddlStartDestination.DataBind();
        Utl.SetNonSelectToControl(ddlStartDestination, MultipleLanguage.DDL_SELECTVALUE[nCurrentLang], "0");
    }
    private void BindDataArrivedDestinationToDropdownList()
    {
        TourDestination obj = new TourDestination();
        DataTable dtb = new DataTable();

        dtb = obj.GetALL();
        ddlArrivedDestination.DataSource = dtb;
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLang)
        {
            case 1:
                ddlArrivedDestination.DataTextField = "TourDestinationTitle1";
                break;
            case 2:
                ddlArrivedDestination.DataTextField = "TourDestinationTitle2";
                break;
            case 3:
                ddlArrivedDestination.DataTextField = "TourDestinationTitle3";
                break;
        }
        ddlArrivedDestination.DataValueField = "TourDestinationID";
        ddlArrivedDestination.DataBind();
        Utl.SetNonSelectToControl(ddlArrivedDestination, MultipleLanguage.DDL_SELECTVALUE[nCurrentLang], "0");
    }
    private void BindDataToCountryDropdown()
    {
        DataTable dtbCountry = new DataTable();
        Country objCountry = new Country();
        dtbCountry = objCountry.GetALL();

        ddlCountry.DataSource = dtbCountry;
        ddlCountry.DataValueField = "CountryID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlCountry.DataTextField = "CountryName1";
                break;
            case 2:
                ddlCountry.DataTextField = "CountryName2";
                break;
            case 3:
                ddlCountry.DataTextField = "CountryName3";
                break;
        }
        ddlCountry.DataBind();
        Utl.SetNonSelectToControl(ddlCountry, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllValueItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToRegionDropdown(int nCountry)
    {
        DataTable dtbRegion = new DataTable();
        Region objRegion = new Region();
        dtbRegion = objRegion.SearchByCountry(nCountry);
        ddlRegion.DataSource = dtbRegion;
        ddlRegion.DataValueField = "RegionID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlRegion.DataTextField = "RegionName1";
                break;
            case 2:
                ddlRegion.DataTextField = "RegionName2";
                break;
            case 3:
                ddlRegion.DataTextField = "RegionName3";
                break;
        }
        ddlRegion.DataBind();
        Utl.SetNonSelectToControl(ddlRegion, MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllValueItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToCatelogyDropdown(int nRegion, int nCountry)
    {
        DataTable dtbTourCat = new DataTable();
        TourCategory objTourCat = new TourCategory();
        dtbTourCat = objTourCat.Search(nRegion, nCountry);
        ddlTourCat.DataSource = dtbTourCat;
        ddlTourCat.DataValueField = "TourCategoryID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlTourCat.DataTextField = "TourCategoryName1";
                break;
            case 2:
                ddlTourCat.DataTextField = "TourCategoryName2";
                break;
            case 3:
                ddlTourCat.DataTextField = "TourCategoryName3";
                break;
        }
        ddlTourCat.DataBind();
        Utl.SetNonSelectToControl(ddlTourCat, MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllValueItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataSafeOffToDropdownList()
    {
        SaleOff obj = new SaleOff();
        DataTable dtb = new DataTable();

        dtb = obj.GetALL();
        ddlSaleOff.DataSource = dtb;
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLang)
        {
            case 1:
                ddlSaleOff.DataTextField = "Title1";
                break;
            case 2:
                ddlSaleOff.DataTextField = "Title2";
                break;
            case 3:
                ddlSaleOff.DataTextField = "Title3";
                break;
        }
        ddlSaleOff.DataValueField = "SaleOffID";
        ddlSaleOff.DataBind();
        Utl.SetNonSelectToControl(ddlSaleOff, MultipleLanguage.DDL_SELECTVALUE[nCurrentLang], "0");
    }

    private void BindDataPriorityDropdown()
    {
        DataTable dtbTours = new DataTable();
        Tours objTour = new Tours();

        dtbTours = objTour.GetAll();
        int nCount = dtbTours.Rows.Count;
        string sAction = GetQuerryString(Config.ACTION);//Lay Thong tin Edit, Insert
        int nRealValue = 0;
        if (sAction == Config.ACTION_EDIT)//Kiem tra dieu kien
        {
            eTour eTourEntity = this.TourEntity();
            if (ConvertData.ConvertToInt(eTourEntity.Priority) < nCount)
            {
                nRealValue = nCount;
            }
            else
            {
                nRealValue = ConvertData.ConvertToInt(eTourEntity.Priority);
            }
            for (int i = 0; i < nRealValue; i++)
            {
                ddlPriority.Items.Add(ConvertData.ConvertToString(i + 1));
            }

           
            string sPriority = ConvertData.ConvertToString(eTourEntity.Priority);
            ddlPriority.SelectedValue = sPriority;
        }
        else
        {
            nRealValue = nCount + 1;
            for (int i = 0; i < nRealValue; i++)
            {
                ddlPriority.Items.Add(ConvertData.ConvertToString(i + 1));
            }
            ddlPriority.SelectedValue = ConvertData.ConvertToString(nRealValue);
        }
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindDataStartDestinationToDropdownList();
        this.BindDataArrivedDestinationToDropdownList();
        this.BindDataSafeOffToDropdownList();
        BindTourTransport();
        BindTourDestination();
        this.InitData();
    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCountry = ConvertData.ConvertToInt(ddlCountry.SelectedValue);
        BindDataToRegionDropdown(nCountry);
        int nRegion = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        BindDataToCatelogyDropdown(nRegion, nCountry);
    }
    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCountry = ConvertData.ConvertToInt(ddlCountry.SelectedValue);
        int nRegion = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        BindDataToCatelogyDropdown(nRegion, nCountry);
    }
    #endregion
    
}